package cn.leida.ruoyi.web.controller.system;

import cn.leida.ruoyi.common.config.AppConfig;
import cn.leida.ruoyi.common.constant.Constants;
import cn.leida.ruoyi.common.core.domain.AjaxResult;
import cn.leida.ruoyi.common.core.domain.entity.SysMenu;
import cn.leida.ruoyi.common.core.domain.entity.SysUser;
import cn.leida.ruoyi.common.core.domain.model.LoginBody;
import cn.leida.ruoyi.common.core.domain.model.LoginUser;
import cn.leida.ruoyi.common.utils.SecurityUtils;
import cn.leida.ruoyi.common.utils.ip.LocalIPUtil;
import cn.leida.ruoyi.framework.web.service.SysLoginService;
import cn.leida.ruoyi.framework.web.service.SysPermissionService;
import cn.leida.ruoyi.framework.web.service.TokenService;
import cn.leida.ruoyi.system.service.ISysMenuService;
import cn.leida.ruoyi.system.service.ISysUserService;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysUserService userService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        loginBody.trimData();
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        try {
            String token = loginService.login(loginBody.getUsername(),
                loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
            ajax.put(Constants.TOKEN, token);
        } catch (Throwable e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        if (!loginUser.getPermissions().equals(permissions)) {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        user.setPassword("");
        if (AppConfig.showDoc()) {
            user.setShowDoc(true);
        }
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * 获取局域网IP地址
     *
     * @return 获取局域网IP地址
     */
    @GetMapping("localIP")
    public AjaxResult localIP() {
        String localIP = LocalIPUtil.localIP();
        JSONObject data = new JSONObject();
        data.put("netIP", localIP);
        return AjaxResult.success(data);
    }

}
